﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace Util
{
    public class Action : System.Web.UI.Page
    {
        
        public void pageIndexChanging(System.Web.UI.WebControls.GridView gridView, GridViewPageEventArgs e)
        {
            DataTable dataTable = (DataTable) gridView.DataSource ;
            gridView.PageIndex = e.NewPageIndex;
            gridView.DataSource = dataTable;
            gridView.DataBind();
        }

        public void sorting(System.Web.UI.WebControls.GridView gridView, GridViewSortEventArgs e)
        {

            DataTable dataTable = (DataTable) gridView.DataSource;

            if (dataTable != null)
            {
                DataView dataView = new DataView(dataTable);
                dataView.Sort = e.SortExpression + " " + this.sortProperty(e.SortExpression);
                gridView.DataSource = dataView;
                gridView.DataBind();
            }

        }

        private string sortProperty(string column)
        {
            string sortDirection = "DESC";

            string sortExpression = ViewState["SortExpression"] as string;

            if (sortExpression != null)
            {

                if (sortExpression == column)
                {
                    string lastDirection = ViewState["SortDirection"] as string;
                    if ((lastDirection != null) && (lastDirection == "DESC"))
                    {
                        sortDirection = "ASC";
                    }
                }
            }

            ViewState["SortDirection"] = sortDirection;
            ViewState["SortExpression"] = column;

            return sortDirection;
        }

    }
    
}